package org.example.controller;

import org.example.pojo.Invitation;
import org.example.pojo.Replay;
import org.example.pojo.Result;
import org.example.service.InvitationService;
import org.example.service.ReplayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/replay")
public class ReplayController {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ReplayService replayService;

    @Autowired
    private InvitationService invitationService;

    /**
     * 查询 某个帖子 的回复
     */
    @GetMapping("/{invitationId}")
    public Result getReplays(@PathVariable("invitationId") Integer invitationId) {
        Invitation invitation = mongoTemplate.findById(invitationId, Invitation.class);
        List<Replay> replays = invitation.getReplays();
        if (replays == null || replays.size() == 0) {
            replays = invitationService.getById(invitationId).getReplays();
        }
        return Result.success(replays);
    }

    /**
     * 添加回复
     */
    @PostMapping
    public Result addReplay(Replay replay) {
        // 插入到 mysql 中
        replayService.save(replay);
        // 条件 -> 获取主帖
        Query query = Query.query(Criteria.where("_id").is(replay.getInvitationId()));
        Invitation invitation = mongoTemplate.findOne(query, Invitation.class);
        List<Replay> replays = invitation.getReplays();
        if (replays == null) {
            replays = new ArrayList<>();
        }
        replays.add(replay);
        mongoTemplate.save(invitation);
        return Result.success(replay);
    }

    /**
     * 修改回复
     */
    @PutMapping
    public Result changeReplay(Replay replay) {
        System.err.println(replay);
        // 修改 mysql 中的
        replayService.updateById(replay);
        // 修改 mongodb 中的
        Query query = Query.query(Criteria.where("id").is(replay.getInvitationId()));
        Invitation invitation = mongoTemplate.findOne(query, Invitation.class);
        List<Replay> replays = invitation.getReplays();
        for (int i = 0; i < replays.size(); i++) {
            if (replays.get(i).getId().equals(replay.getId())) {
                replays.set(i, replay);
            }
        }
        mongoTemplate.save(invitation);
        return Result.success(replay);
    }

    /**
     * 删除回复
     */
    @DeleteMapping
    public Result deleteReplay(Replay replay) {
        // 先通过 replay 获取 mongodb 中的 invitation
        Invitation invitation = mongoTemplate.findById(replay.getInvitationId(), Invitation.class);
        // 删除 mysql 中的
        replayService.removeById(replay.getId());
        // 删除 mongodb 中的
        List<Replay> replays = invitation.getReplays();
        for (int i = 0; i < replays.size(); i++) {
            if (replays.get(i).getId().equals(replay.getId())) {
                replays.remove(replays.get(i));
            }
        }
        mongoTemplate.save(invitation);
        return Result.success(replay);
    }
}
